package com.qf.flash.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qf.flash.R;


public class RatingBarView extends LinearLayout
{

    public interface OnRatingListener
    {
        void onRating(Object bindObject, int RatingScore);
    }

    private boolean mClickable = true;
    private OnRatingListener onRatingListener;
    private Object bindObject;
    private float starImageSize;
    private int starCount;
    private Drawable starEmptyDrawable;
    private Drawable starFillDrawable;
    private int mStarCount;
    private float starMargin;
    private boolean starIsIndicator;
    private Context mContext;

    @Override
    public void setClickable(boolean clickable)
    {
        this.mClickable = clickable;
    }

    public RatingBarView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setOrientation(LinearLayout.HORIZONTAL);
        mContext = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RatingBarView);
        starImageSize = ta.getDimension(R.styleable.RatingBarView_starImageSize, 20);
        starCount = ta.getInteger(R.styleable.RatingBarView_starCount, 5);
        starEmptyDrawable = ta.getDrawable(R.styleable.RatingBarView_starEmpty);
        starFillDrawable = ta.getDrawable(R.styleable.RatingBarView_starFill);
        starMargin = ta.getDimension(R.styleable.RatingBarView_starMargin, 0);
        starIsIndicator = ta.getBoolean(R.styleable.RatingBarView_starIsIndicator, true);
        ta.recycle();

        if (starCount > 5)
        { return;}
        for (int i = 0; i < starCount; ++i)
        {
            ImageView imageView = getStarImageView(context, i == starCount - 1);
            if (starIsIndicator)
            {
                imageView.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        if (mClickable)
                        {
                            mStarCount = indexOfChild(v) + 1;
                            setStar(mStarCount, false);
                            if (onRatingListener != null)
                            {
                                onRatingListener.onRating(bindObject, mStarCount);
                            }
                        }
                    }
                });
            }
            addView(imageView);
        }
    }

    private ImageView getStarImageView(Context context, boolean isLastOne)
    {
        ImageView imageView = new ImageView(context);
        int margin = Math.round(starMargin);
        if (isLastOne)
        {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
//                    Math.round(starImageSize),
//                    Math.round(starImageSize)
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            imageView.setLayoutParams(params);
            imageView.setPadding(0, 0, 0, 0);
            imageView.setImageDrawable(starEmptyDrawable);
        }
        else
        {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
//                    Math.round(starImageSize + starMargin),
//                    Math.round(starImageSize)
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            imageView.setLayoutParams(params);
            imageView.setPadding(0, 0, margin, 0);
            imageView.setImageDrawable(starEmptyDrawable);
        }
        return imageView;
    }

    public void setStar(int starCount, boolean animation)
    {
        starCount = starCount > this.starCount ? this.starCount : starCount;
        starCount = starCount < 0 ? 0 : starCount;
        for (int i = 0; i < starCount; ++i)
        {
            ((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
            if (animation)
            {
                ScaleAnimation sa = new ScaleAnimation(0, 0, 1, 1);
                getChildAt(i).startAnimation(sa);
            }
        }
        for (int i = this.starCount - 1; i >= starCount; --i)
        {
            ((ImageView) getChildAt(i)).setImageDrawable(starEmptyDrawable);
        }
    }

    public int getStarCount()
    {
        return mStarCount;
    }

    public void setStarFillDrawable(Drawable starFillDrawable)
    {
        this.starFillDrawable = starFillDrawable;
    }

    public void setStarEmptyDrawable(Drawable starEmptyDrawable)
    {
        this.starEmptyDrawable = starEmptyDrawable;
    }

    public void setStarCount(int starCount)
    {
        this.starCount = starCount;
        for (int i = 0; i < starCount; ++i)
        {
            ImageView imageView = getStarImageView(mContext, i == starCount - 1);
            addView(imageView);
        }
    }

    public void setStarImageSize(float starImageSize)
    {
        this.starImageSize = starImageSize;
    }

    public void setBindObject(Object bindObject)
    {
        this.bindObject = bindObject;
    }

    public void setOnRatingListener(OnRatingListener onRatingListener)
    {
        this.onRatingListener = onRatingListener;
    }
}
